
'use strict';
define(['app', 'utils/route-config'], function (app, routeConfig) {

    /**
	* Documents services
	*/
	app.factory("documentServices", function($http) {
		var documents = undefined;
		return {
			initialize: function(callback) {
				console.log('get documents');
				$http.post(app.serviceBase, { "service" : "getDocuments", "root" : "", "forceToRefresh" : true, "filter" : "" }).
				success(function(data, status) 
				{
					console.info("getDocuments - call success"); 
					if(data.isFailed == true)
					{
						throw data.exception;
					}
					documents = data.response;
					callback(documents);
				})
				.
				error(function(data, status) 
				{
					console.error("getDocuments - call failed"); 	
					throw status + ' : ' + data;		
				});
			},
			getDocuments: function(callback, path, filter) {
				$http.post(app.serviceBase, { "service" : "getDocuments", "root" : path, "forceToRefresh" : true, "filter" : filter }).
				success(function(data, status) 
				{
					console.info("getDocuments - call success"); 
					if(data.isFailed == true)
					{
						throw data.exception;
					}
					documents = data.response;
					callback(documents);
				})
				.
				error(function(data, status) 
				{
					console.error("getDocuments - call failed"); 	
					throw status + ' : ' + data;		
				});
			},
			upload: function(callback, file, rootpath) {
				console.info("documentServices.upload - called");
				var fd = new FormData();
				fd.append("service", "uploadDocument");
				fd.append("rootpath", rootpath);
				fd.append("file", file);
				
				$http.post(app.serviceBase,
					fd,
					{ 
						withCredentials: true, headers: {'Content-Type': undefined }, transformRequest: angular.identity 
					}
				).
				success(function(data, status) 
				{
					console.info("documentServices.upload - call success"); 
					callback(data);
				})
				.
				error(function(data, status) 
				{
					console.error("documentServices.upload - call failed"); 	
					throw status + ' : ' + data;		
				});
			},
			createFolder: function(callback, rootpath, foldername) {
				console.info("documentServices.createFolder - is calling with " + rootpath + " " + foldername); 	
				$http.post(app.serviceBase, { "service" : "createFolder", "path" : rootpath, "name": foldername }).
				success(function(data, status) 
				{
					console.info("documentServices.createFolder - call success"); 	
					callback(data);
				})
				.
				error(function(data, status) 
				{
					console.error("documentServices.createFolder - call failed"); 	
					throw status + ' : ' + data;		
				});
			},
			deleteDocument: function(callback, path) {
				$http.post(app.serviceBase, { "service" : "deleteDocument", "path" : path }).
				success(function(data, status)
				{
					console.info("deleteDocumentCommand - call sucess"); 
					callback(data);
				})
				.
				error(function(data, status) 
				{
					console.error("documentServices.deleteDocumentCommand - call failed"); 	
					throw status + ' : ' + data;						
				});
			},
			rename: function(callback, path, newname, oldname) {
				alert(path);
				$http.post(app.serviceBase, { "service" : "renameDocument", "path" : path, "newName" : newname, "oldName" : oldname }).
				success(function(data, status) 
				{
					console.info("renameDocumentCommand - call success"); 	
					callback(data);
				})
				.
				error(function(data, status) 
				{
					console.error("documentServices.renameDocumentCommand - call failed"); 	
					throw status + ' : ' + data;				
				});
			},
			download: function(callback, path) {
				$http.post(app.serviceBase, { "service" : "createTempDocument", "sourcePath" : String(path) }).
				success(function(data, status) 
				{
					console.info("download - call sucess");
					callback(data);
				})
				.
				error(function(data, status) 
				{
					console.error("documentServices.download - call failed"); 	
					throw status + ' : ' + data;				
				});
			}
		};
	});

    return app;
});